package com.gxa.learning.portal.service.impl;

import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.Feedback;
import com.gxa.learning.core.pojo.ProjectSign;
import com.gxa.learning.core.service.impl.BaseServiceImpl;
import com.gxa.learning.core.util.StringUtil;
import com.gxa.learning.portal.mapper.SignInMapper;
import com.gxa.learning.portal.mapper.StudyPlanMapper;
import com.gxa.learning.portal.service.StudyPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @version E-learning
 * @Author lonny
 * @Date 2021/12/13 20:12
 */
@Service
public class StudyPlanServiceImpl extends BaseServiceImpl<Feedback> implements StudyPlanService {

    @Autowired
    private StudyPlanMapper studyPlanMapper;

    @Autowired
    private SignInMapper signInMapper;


    /**
     * 查询对应用户所有培训项目列表
     *
     * @param userId 培训人id
     * @return 列表
     */
    @Override
    public List<Map<String, Object>> getUserProjectList(String userId) throws Exception {
        if (StringUtil.isBlankOrNull(userId)) {
            throw new BusinessException(ExceptionCode.USER_OR_PROJECT_WRONG);
        }
        List<Map<String, Object>> userProjectList = studyPlanMapper.getUserProjectList(userId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        userProjectList.forEach(map -> map.put("project_endTime", simpleDateFormat.format(map.get("project_endTime"))));
        return userProjectList;
    }

    /**
     * 查询项目所有课程
     *
     * @param projectId 项目编号
     * @param userId    用户编号
     * @return 列表
     */
    @Override
    public List<Map<String, Object>> getCourseList(String projectId, String userId) throws Exception {

//        if (StringUtil.isBlankOrNull(projectId) || StringUtil.isBlankOrNull(userId)) {
//            throw new BusinessException(ExceptionCode.USER_OR_PROJECT_WRONG);
//        }
        return studyPlanMapper.getCourseList(projectId, userId);
    }


    /**
     * 签到表
     *
     * @param userId    用户编号
     * @param projectId 项目编号
     * @param courseId  课程编号
     * @return lb
     */
    @Override
    public List<Map<String, Object>> getSignInList(String userId, String projectId, String courseId) throws Exception {


        if (StringUtil.isBlankOrNull(projectId) || StringUtil.isBlankOrNull(userId)) {
            throw new BusinessException(ExceptionCode.USER_OR_PROJECT_WRONG);
        }
        return studyPlanMapper.getSignInList(userId, projectId, courseId);
    }


    @Override
    public void signIn(Long pid, Long uid, String key) throws BusinessException {
        if (pid == null || uid == null || StringUtil.isEmpty(key)) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }
        ProjectSign projectSign = new ProjectSign();
        projectSign.setUserId(uid);
        projectSign.setProjectId(pid);
        projectSign.setSignKey(key);
        Example example = new Example(ProjectSign.class);
        Example.Criteria select = example.createCriteria();
        select.andEqualTo("signKey", key);
        select.andEqualTo("userId", uid);
        select.andEqualTo("projectId", pid);
        ProjectSign isHave = signInMapper.selectOneByExample(example);
        if (isHave == null) {
            throw new BusinessException(ExceptionCode.ID_WRONG);
        }
        if (isHave.getStatus()) {
            throw new BusinessException(ExceptionCode.HAD_SIGN_IN);
        }
        projectSign.setSignInId(isHave.getSignInId());
        projectSign.setStatus(true);
        projectSign.setSignTime(new Date());
        signInMapper.update(projectSign);
    }


    /**
     * 通过用户和培训 查询反馈情况
     *
     * @param userId    用户
     * @param projectId 培训
     * @param courseId  培训
     * @return 返回
     * @throws Exception pc
     */
    @Override
    public Map<String, Object> findByUserAndProject(Long userId, Long projectId, Integer courseId) throws Exception {
        Map<String, Object> byUserAndProject;
        if (projectId != null && courseId != null) {
            System.out.println("projectId != null && courseId != null");
            throw new BusinessException(ExceptionCode.FEED_WRONG);
        } else if (projectId == null) {
            System.out.println("projectId == null");
            byUserAndProject = signInMapper.findByUserAndProject(userId, null, courseId);
        } else {
            System.out.println("courseId==null");
            byUserAndProject = signInMapper.findByUserAndProject(userId, projectId, null);
        }
        if (projectId == null && courseId == null) {
            throw new BusinessException(ExceptionCode.FEED_WRONG);
        }
        System.out.println(byUserAndProject);
        String num = "1";
        if (byUserAndProject == null) {
            throw new BusinessException(ExceptionCode.ID_WRONG);
        }
        String i = "id";

        if (byUserAndProject.get(i) == null || StringUtil.isBlankOrNull(byUserAndProject.get(i).toString())) {
            throw new BusinessException(ExceptionCode.FEEDBACK_NUM);
        }
        String id = byUserAndProject.get(i).toString();
        //      判断是否输入的为纯数字
        if (!StringUtil.isNum(id)) {
            throw new BusinessException(ExceptionCode.WRONG_NUM);
        }
        System.out.println(id);
        String s = byUserAndProject.get("state").toString();
        if (StringUtil.isBlankOrNull(s)) {
            throw new BusinessException(ExceptionCode.ID_WRONG);
        }
        if (num.equals(s)) {
            throw new BusinessException(ExceptionCode.REMIND_USER);
        }

        return byUserAndProject;
    }


    @Override
    public Map<String, Object> getById(String id) throws BusinessException {
        System.out.println("id = " + id);
        boolean blankOrNull = StringUtil.isBlankOrNull(id);
        // boolean blankOrNull = StringUtil.isEmpty(id);
        System.out.println(blankOrNull);
        Boolean num = StringUtil.isNum(id);
        System.out.println(num);
        if (blankOrNull || Boolean.TRUE.equals(!num)) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }
        Map<String, Object> byId = signInMapper.getById(id);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
        byId.put("project_endTime", simpleDateFormat.format(byId.get("project_endTime")));
        return byId;
    }

    /**
     * 课程评分
     *
     * @param courseId 课程
     * @return Map
     */
    @Override
    public Map<String, Object> courseNum(String courseId) {
        return signInMapper.courseNum(courseId);
    }
}
